const http = require('http')
const fs = require('fs')

const server = http.createServer((req, res) => {
    console.log('url: ', req.url)
    // console.log(req.headers);
    if (req.url === '/getSomething') {
        res.setHeader("Access-Control-Allow-Origin", "http://localhost:4000")
        res.setHeader("Access-Control-Allow-Methods", "PUT")
        res.setHeader("Access-Control-Allow-Credentials", true)
        res.setHeader('Set-Cookie', 'z=1; HttpOnly')
        res.end('getSomething请求成功！');
    }

    if (req.url === '/index.html') {
        fs.readFile('./index.html', (err, data) => {
            res.writeHead(200, {
                'Content-Type': 'text/html'
            })
            res.write(data.toString())
            res.end()
        })
    }

    if (req.url === '/login') {
        res.setHeader('Set-Cookie', 'nodeToken=1111; HttpOnly')
        res.end('login success')
    }

    if (req.url.includes('/transfer')) {
        console.log(req.headers.cookie)
        if (req.headers.cookie.includes('nodeToken=1111')) {
            res.end('transfer success')
        }
        
    }
})

server.listen(8000)
console.log('ok')
